package com.lyshop.guiyingshanding.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.tencent.cloud.huiyansdkface.facelight.api.WbCloudFaceContant;
import com.tencent.cloud.huiyansdkface.facelight.api.WbCloudFaceVerifySdk;
import com.tencent.cloud.huiyansdkface.facelight.api.listeners.WbCloudFaceVerifyLoginListener;
import com.tencent.cloud.huiyansdkface.facelight.api.listeners.WbCloudFaceVerifyResultListener;
import com.tencent.cloud.huiyansdkface.facelight.api.result.WbFaceError;
import com.tencent.cloud.huiyansdkface.facelight.api.result.WbFaceVerifyResult;
import com.tencent.cloud.huiyansdkface.facelight.process.FaceVerifyStatus;
import com.lyshop.guiyingshanding.bean.TxLiveTokenBean;
import com.lyshop.guiyingshanding.utils.LogUtil;
import com.lyshop.guiyingshanding.utils.RouteUtil;

public class BindCardFaceVerifyActivity extends Activity {

    private String mName;
    private String mCertId;
    private TxLiveTokenBean mLiveBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mName = getIntent().getStringExtra("name");
        mCertId = getIntent().getStringExtra("cert");
        mLiveBean = (TxLiveTokenBean) getIntent().getSerializableExtra(RouteUtil.TXLIVE_TOKEN_MODEL);

        openCloudFaceService();
    }

    //拉起刷脸sdk
    public void openCloudFaceService() {
        Bundle data = new Bundle();
        WbCloudFaceVerifySdk.InputData inputData = new WbCloudFaceVerifySdk.InputData(
                mLiveBean.faceId,
                mLiveBean.orderNo,
                mLiveBean.appId,
                mLiveBean.version,
                mLiveBean.nonce,
                mLiveBean.userId,
                mLiveBean.nonceSign,
                FaceVerifyStatus.Mode.GRADE,
                mLiveBean.license);

        LogUtil.d("faceId = " + mLiveBean.faceId + " orderNo = " + mLiveBean.orderNo + " appId = " + mLiveBean.appId + " version = " + mLiveBean.version + " nonce = " + mLiveBean.nonce + " userId = " +
                mLiveBean.userId + " sign = " + mLiveBean.nonceSign + " license = " + mLiveBean.license);

        data.putSerializable(WbCloudFaceContant.INPUT_DATA, inputData);

        //sdk样式设置，默认为白色
        data.putString(WbCloudFaceContant.COLOR_MODE, WbCloudFaceContant.WHITE);
        //定制合作方个性化提示语，默认不设置
        data.putString(WbCloudFaceContant.CUSTOMER_TIPS_UPLOAD, "已提交审核，请等待结果");
        //设置合作方定制提示语的位置，默认为识别框的下方
        //识别框的下方： WbCloudFaceContant.CUSTOMER_TIPS_LOC_BOTTOM
        //识别框的上方：WbCloudFaceContant.CUSTOMER_TIPS_LOC_TOP
        //此处设置为识别框的上方
        data.putInt(WbCloudFaceContant.CUSTOMER_TIPS_LOC, WbCloudFaceContant.CUSTOMER_TIPS_LOC_TOP);
        //设置选择的比对类型  默认为权威库网纹图片比对
        //此处设置权威数据源对比
        data.putString(WbCloudFaceContant.COMPARE_TYPE, WbCloudFaceContant.ID_CARD);
        //是否需要录制上传视频 默认不需要，此处设置为不需要
        data.putBoolean(WbCloudFaceContant.VIDEO_UPLOAD, false);
        //是否对录制视频进行检查,默认不检查，此处设置为不检查
        data.putBoolean(WbCloudFaceContant.VIDEO_CHECK, false);
        //设置是否打开语音提示，默认关闭，此处设置为关闭
        data.putBoolean(WbCloudFaceContant.PLAY_VOICE, false);

        WbCloudFaceVerifySdk.getInstance().initAdvSdk(BindCardFaceVerifyActivity.this, data, new WbCloudFaceVerifyLoginListener() {
            @Override
            public void onLoginSuccess() {
                //登录sdk成功
                LogUtil.i("dangzt", "onLoginSuccess");

                //拉起刷脸页面
                WbCloudFaceVerifySdk.getInstance().startWbFaceVerifySdk(BindCardFaceVerifyActivity.this, new WbCloudFaceVerifyResultListener() {
                    @Override
                    public void onFinish(WbFaceVerifyResult result) {
                        //得到刷脸结果
                        if (result != null) {
                            if (result.isSuccess()) {
                                String resultStr = "刷脸成功! Sign=" + result.getSign() + "; liveRate=" + result.getLiveRate() +
                                        "; similarity=" + result.getSimilarity() + "riskInfo=" + result.getRiskInfo().toString() + " 照片" + result.getUserImageString();
                                LogUtil.d("dangzt", resultStr);
//                                    Toast.makeText(FaceVerifyDemoActivity.this, "刷脸成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent();
                                intent.putExtra(RouteUtil.BUNDLE_LIVE_RESULT, resultStr);
                                intent.putExtra(RouteUtil.BUNDLE_LIVE_IS_SUCCESS, true);
                                setResult(RESULT_OK, intent);
                                finish();
                            } else {
                                WbFaceError error = result.getError();
                                if (error != null) {
                                    String resultStr = "刷脸失败！domain=" + error.getDomain() + ";code=" + error.getCode()
                                            + ";desc=" + error.getDesc() + ";reason=" + error.getReason();
                                    LogUtil.d("dangzt", resultStr);
                                    if (error.getDomain().equals(WbFaceError.WBFaceErrorDomainCompareServer)) {
                                        LogUtil.d("dangzt", "对比失败，liveRate=" + result.getLiveRate() +
                                                "; similarity=" + result.getSimilarity());
                                    }
                                    Intent intent = new Intent();
//                                    intent.putExtra(RouteUtil.BUNDLE_LIVE_RESULT, resultStr);
//                                    intent.putExtra(RouteUtil.BUNDLE_LIVE_IS_SUCCESS, false);
                                    setResult(RESULT_OK, intent);
                                    finish();
                                } else {
                                    LogUtil.e("dangzt", "sdk返回error为空！");
                                }
                            }
                        } else {
                            LogUtil.e("dangzt", "sdk返回结果为空！");
                        }

                        //更新userId
                        LogUtil.d("dangzt", "更新userId");
//                        userId = "WbFaceVerifyREF" + System.currentTimeMillis();
                    }
                });
            }

            @Override
            public void onLoginFailed(WbFaceError error) {
                //登录失败
                LogUtil.i("dangzt", "onLoginFailed!");
//                progressDlg.dismiss();
//                finish();
//                if (error != null) {
                LogUtil.d("dangzt", "登录失败！domain=" + error.getDomain() + " ;code= " + error.getCode()
                        + " ;desc=" + error.getDesc() + ";reason=" + error.getReason());
                if (error.getDomain().equals(WbFaceError.WBFaceErrorDomainParams)) {
                    Toast.makeText(BindCardFaceVerifyActivity.this, "传入参数有误！" + error.getDesc(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(BindCardFaceVerifyActivity.this, "登录刷脸sdk失败！" + error.getDesc(), Toast.LENGTH_SHORT).show();
                }
//                } else {
//                    LogUtil.e(TAG, "sdk返回error为空！");
//                }
            }
        });
    }
}
